package com.symbio.ishenapi.patient.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.symbio.ishenapi.patient.bean.PatientBloodPressureResponse;
import com.symbio.ishenapi.patient.bean.PatientBloodSugar;
import com.symbio.ishenapi.patient.bean.PatientBloodSugarAttr;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientWeight;
import io.jboot.db.annotation.Table;
import com.symbio.ishenapi.patient.model.base.BaseTmPatientBloodSugar;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Generated by Jboot.
 */
@Table(tableName = "tm_patient_blood_sugar", primaryKey = "id")
public class TmPatientBloodSugar extends BaseTmPatientBloodSugar<TmPatientBloodSugar> {
    public static TmPatientBloodSugar dao = new TmPatientBloodSugar();

    /**
     * @return
     * @Author GuoShunYou
     * @Description 通过userId、runtime（日期）查询血糖记录
     * @date 下午 2:06   2018/10/23
     */
    public PatientBloodSugar findTmPatientBloodPressure(PatientBloodSugar pasugar) {
        String sql = "SELECT s.userId userid,s.runtime runtime,sa.sugarType sugartype,sa.sugarValue val,sa.timequantum timequantum from tm_patient_blood_sugar s JOIN tm_patient_blood_sugar_attr sa  ON s.id=sa.sugarId where s.status=1 and s.runtime='" + pasugar.getRuntime() + "' and s.userId=" + pasugar.getUserId() + "";
        List<Record> list = Db.find(sql);
        PatientBloodSugar p = new PatientBloodSugar();
        if (list == null) {
            return p;
        }
        List<PatientBloodSugarAttr> listpattr = new ArrayList<>();
        int userId = 0;
        String runtime = "";
        int coun = 0;
        PatientBloodSugarAttr pattr = new PatientBloodSugarAttr();
        for (int i = 0; i < list.size(); i++) {
            coun++;
            userId = Integer.valueOf(list.get(i).getStr("userid"));
            runtime = list.get(i).getStr("runtime");
            if ("200000027".equals(list.get(i).getStr("sugartype"))) {
                pattr.setLimosis(list.get(i).getStr("val"));
            } else if ("200000028".equals(list.get(i).getStr("sugartype"))) {
                pattr.setBreakfastLater(list.get(i).getStr("val"));
            } else if ("200000029".equals(list.get(i).getStr("sugartype"))) {
                pattr.setLunchBefore(list.get(i).getStr("val"));
            } else if ("200000030".equals(list.get(i).getStr("sugartype"))) {
                pattr.setLunchLater(list.get(i).getStr("val"));
            } else if ("200000031".equals(list.get(i).getStr("sugartype"))) {
                pattr.setDinnerBefore(list.get(i).getStr("val"));
            } else if ("200000032".equals(list.get(i).getStr("sugartype"))) {
                pattr.setDinnerLater(list.get(i).getStr("val"));
            } else if ("200000033".equals(list.get(i).getStr("sugartype"))) {
                pattr.setSleepBefore(list.get(i).getStr("val"));
            }
            pattr.setTime(list.get(i).getStr("timequantum"));
            if (coun % 7 == 0) {
                System.out.println(coun);
                System.out.println(pattr.getLimosis() + "  " + pattr.getBreakfastLater() + "  " + pattr.getTime());
                listpattr.add(pattr);
                pattr = new PatientBloodSugarAttr();
            }
        }
        String usid = String.valueOf(userId);
        BigInteger bigint = new BigInteger(usid);
        p.setUserId(bigint);
        p.setList(listpattr);
        p.setRuntime(runtime);
        return p;
    }


    /**
     * @return java.util.List<com.symbio.ishenapi.patient.bean.PatientBloodPressureResponse>
     * @Author GuoShunYou
     * @Description 通過userId查詢所有血糖记录
     * @date 下午 5:38   2018/10/23
     */
    public List<PatientBloodPressureResponse> findAllSugar(TmPatientBloodSugar tmp) {
        String sql = "SELECT sa.id ,s.userId userId,s.runtime runtime,sa.sugarType sugarType,sa.sugarValue sugarValue,sa.timequantum timequantum from tm_patient_blood_sugar s,tm_patient_blood_sugar_attr sa where s.id=sa.sugarId and s.userId=" + tmp.getUserId() + " and s.`status`=1 order by s.runtime";
        List<Record> list = Db.find(sql);
        List<PatientBloodPressureResponse> prelist = new ArrayList<PatientBloodPressureResponse>();
        for (int i = 0; i < list.size(); i++) {
            PatientBloodPressureResponse pa = new PatientBloodPressureResponse();
            String userid = list.get(i).getStr("userId");
            pa.setUserId(Integer.valueOf(userid));
            String runtime = list.get(i).getStr("runtime");
            pa.setRuntime(runtime);
            String pressureType = list.get(i).getStr("sugarType");
            pa.setPressureType(pressureType);
            String pressureValue = list.get(i).getStr("sugarValue");
            pa.setPressureValue(pressureValue);
            String timequantum = list.get(i).getStr("timequantum");
            pa.setTimequantum(timequantum);
            prelist.add(pa);
        }
        return prelist;

    }

    /**
     * @return
     * @Author GuoShunYou
     * @Description 根據userID查詢日期分組
     * @date 下午 8:55   2018/10/23
     */
    public List<String> getRuntime(TmPatientBloodSugar tmp) {
        String sql = "SELECT runtime runtimes FROM tm_patient_blood_sugar  WHERE userId=" + tmp.getUserId() + " and `status`=1  GROUP BY runtime ORDER BY runtime";
        List<TmPatientBloodSugar> list = dao.find(sql);
        List<String> lis = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            String time = list.get(i).getStr("runtimes");
            lis.add(time);
        }
        return lis;
    }


    /**
     * @return java.util.List<java.lang.String>
     * @Author GuoShunYou
     * @Description 通过血糖主表id获取所关联的附表id集合
     * @date 下午 3:16   2018/10/24
     */
    public List<String> getIdList(Object pressureid) {
        String sql = "select id ids from tm_patient_blood_sugar_attr where sugarId=" + pressureid + "";
        List<TmPatientBloodSugar> lisst = dao.find(sql);
        List<String> li = new ArrayList<String>();
        for (int i = 0; i < lisst.size(); i++) {
            String id = lisst.get(i).getStr("ids");
            li.add(id);
        }
        return li;

    }

    public List<TmPatientBloodSugar> findOneSugar(BaseTmPatientBloodSugar baseTmPatientBloodSugar) {     //查询血糖一条数据
        String sql = ("select * from tm_patient_blood_sugar tpu where tpu.userId = ?  and tpu.status = '1' order by createTime desc LIMIT 1 ");
        List<TmPatientBloodSugar> find = dao.find(sql, baseTmPatientBloodSugar.getUserId());
        return find;
    }

    /**
     * @return int
     * @Author GuoShunYou
     * @Description 通过userid、runtime查询是否有该数据
     * @date 下午 6:02   2018/10/28
     */
    public int selectCount(PatientBloodSugar pa) {
        String sql = "select count(1) con from tm_patient_blood_sugar where userId=" + pa.getUserId() + " and runtime='" + pa.getRuntime() + "'";
        TmPatientBloodSugar tm = dao.findFirst(sql);
        String num = tm.getStr("con");
        int count = Integer.valueOf(num);
        return count;
    }

    /**
     * @return int
     * @Author GuoShunYou
     * @Description 通过userid、runtime查询信息所对应的主id
     * @date 下午 6:09   2018/10/28
     */
    public BigInteger selectId(PatientBloodSugar pa) {
        String sql = "select id from tm_patient_blood_sugar where userId=" + pa.getUserId() + " and runtime='" + pa.getRuntime() + "'";

        TmPatientBloodSugar tmp = dao.findFirst(sql);
        return tmp.getId();


    }
}
